package com.nattakute.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.nattakute.dao.LabDAO;
import com.nattakute.model.Lab;

@Repository
public class LabDAOImpl implements LabDAO{

	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	
	public List<Lab> getLab() {
		// TODO Auto-generated method stub
		return sessionFactory.getCurrentSession().createQuery("from Lab l").list();
	}

	
	public Lab getLabById(String id) {
		Lab lab = null;
		try {
			lab = (Lab)sessionFactory.getCurrentSession().createQuery("from Lab where labID= :id").setParameter("id", id).list().get(0);
			return lab;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	
	public Boolean addLab(Lab lab) {
		try {
			sessionFactory.getCurrentSession().save(lab);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	
	public Boolean deleteLab(Lab lab) {
		try {
			sessionFactory.getCurrentSession().delete(lab);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	
	public Boolean updateLab(Lab lab) {
		try {
			sessionFactory.getCurrentSession().update(lab);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	
	public Lab getLabByTimeTableId(String timeTableId) {
		// TODO Auto-generated method stub
		Criteria labCriteria=sessionFactory.getCurrentSession().createCriteria(Lab.class);
		Criteria timeTableCriteria=labCriteria.createCriteria("timetableSet");
		timeTableCriteria.add(Restrictions.eq("timeTableID", timeTableId));
		
		return (Lab) timeTableCriteria.list().get(0);
	}

	@SuppressWarnings("unused")
	
	public List<Lab> getLabs() {
		// TODO Auto-generated method stub
		Criteria labCriteria=sessionFactory.getCurrentSession().createCriteria(Lab.class);
				
		return null;
	}

	@SuppressWarnings("unchecked")
	
	public List<Lab> getLabByDepartmentId(String departmentID) {
		// TODO Auto-generated method stub
		Criteria labCriteria=sessionFactory.getCurrentSession().createCriteria(Lab.class);
		Criteria departmentCriteria=labCriteria.createCriteria("departmentID");
		departmentCriteria.add(Restrictions.eq("departmentID",departmentID ));
		List<Lab> list=(List<Lab>)labCriteria.list();
		return list;
	}
	

	
	@SuppressWarnings("unchecked")
	public List<Lab> getActiveLab(String classID,String shifts,Date enddate) {
		Criteria labCriteria=sessionFactory.getCurrentSession().createCriteria(Lab.class);
		Criteria departmentCriteria=labCriteria.createCriteria("timetableSet");
		Criteria classCriteria=departmentCriteria.createCriteria("classID");
		
		
		classCriteria.add(Restrictions.eq("classID", classID));
		departmentCriteria.add(Restrictions.eq("shifts", shifts));
		departmentCriteria.add(Restrictions.gt("endTime",enddate));
		departmentCriteria.add(Restrictions.eq("haslearn", false));
		
		List<Lab> list=(List<Lab>)labCriteria.list();
		
		Criteria labCriteria1=sessionFactory.getCurrentSession().createCriteria(Lab.class);
		List<Lab> fullLab=(List<Lab>)labCriteria1.list();
		
		for (Lab lab : fullLab) {
			for (Lab lab1 : list) {
				if(lab.equals(lab1)){
					fullLab.remove(lab1);
				}
			}
		}
		return fullLab;
	}

}
